using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MMM.Core.Domain;
using ServiceStack.ServiceHost;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using MMM.Services.Base;
using MMM.Services.Operations;
using MMM.Services.Type;
using MMM.Services.Response;
using ServiceStack.ServiceInterface;
using MMM.Core;

namespace MMM.Services
{

    public class CompanyInfoService : BaseService<CompanyInfo>
    {
    }

    public class CompanyInfoGetSerice : BaseGetService<CompanyInfoPageGet, CompanyInfo>
    {
        //protected override System.Linq.Expressions.Expression<Func<CompanyInfo, bool>> CreateFilter(CompanyInfoPageGet request)
        //{
        //    return base.CreateFilter(request);
        //}
        protected override string CreateFilter(CompanyInfoPageGet request)
        {
            string sql = string.Empty;
            IList<Filter> lst = request.GetFilter();
            if (lst == null)
            {
                return sql;
            }
            foreach (var item in lst)
            {
                if (string.IsNullOrEmpty(sql))
                {
                    if (item.Property == "SuperiorID")
                    {
                        item.Value = Guid.Parse(item.Value).ToString();
                        sql = item.ToSqlEque();
                    }
                    else
                    {
                        sql = item.ToSqlLike();
                    }
                }
                else
                {
                    if (item.Property == "SuperiorID")
                    {
                        item.Value = Guid.Parse(item.Value).ToString();
                        sql = string.Format(" {0} and {1}", sql, item.ToSqlEque());
                    }
                    else
                    {
                        sql = string.Format(" {0} and {1} ", sql, item.ToSqlLike());
                    }
                }
            }
            return sql;
        }
    }

    public class CompanyInfoDeleteService : BaseDeleteService<CompanyInfo, CompanyInfoDelete>
    {
    }
    public class CompanyInfoItemService : RestServiceBase<CompanyInfoItem>
    {
        IRepository<CompanyInfo> comRep = ApplicationContext.Container.GetInstance<IRepository<CompanyInfo>>();
        public override object OnGet(CompanyInfoItem request)
        {
            var node = new CompanyInfoItem { id = Guid.Empty };
            AddCompanyInfo(node, node.id);

            return node;
        }

        private void AddCompanyInfo(CompanyInfoItem node, Guid? nullable)
        {
            var comList = comRep.GetEntities(t => t.SuperiorID == nullable.Value, s => s.ChineseShortName, true);
            if (comList != null)
            {
                foreach (var item in comList)
                {
                    var comNode = new CompanyInfoItem { id = item.Id, expanded = true, leaf = false, level = 1, parentId = nullable, text = item.ChineseFullName };

                    node.children.Add(comNode);

                    AddCompanyInfo(comNode, item.Id);
                }
            }
        }
    }

    [Serializable]
    [Route("/api/companyinfoitem", "GET")]
    public class CompanyInfoItem : JsonNode<CompanyInfoItem>
    {
        public Guid? parentId { get; set; }
    }
}
